package com.sky.utils;

import com.obs.services.ObsClient;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Data
@AllArgsConstructor
@Slf4j
public class HuaWeiObsUtil {
    private String ak;
    private String sk;
    private String endPoint;
    private String bucketname;

    public String upload(MultipartFile file) throws IOException
    {
        //获取上传文件的输入流
        InputStream inputStream = file.getInputStream();
        //设置命名规则避免文件覆盖
        String originalFilename = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //上传文件到OBS
        ObsClient obsClient = new ObsClient(ak, sk,endPoint);
        //采用流式上传
        obsClient.putObject(bucketname,filename,inputStream);
        //文件访问路径
        String url = "https://" + bucketname +"." + "obs.cn-east-5.myhuaweicloud.com" + "/" + filename;
        inputStream.close();
        return url;


    }
}
